﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "IGalleryDefinition.h"
#include "ControlDefinition.h"
#include "IGalleryEvent.h"
#include "WeakPtr.h"
namespace gcmp
{
    class ControlDefinitionChangeEvent;
    class IControlDefinitionChangeEvent;
    class GalleryItemDefinition;
    class GCMP_GUI_BASE_IMPL_EXPORT GalleryDefinition : public IGalleryDefinition
    {
        CANNOT_COPY_OR_ASSIGN(GalleryDefinition);
    public:
        GalleryDefinition(const std::wstring& id, const std::wstring& caption, int columnCount, int itemWidth = 16, int itemHeight = 16);
        virtual ~GalleryDefinition();

        virtual int GetItemWidth() const override { return m_itemWidth; }
        virtual int GetItemHeight() const override { return m_itemHeight; }

        virtual bool SetCurrentGalleryItem(const std::wstring& id) override;

        void SetCurrentIndex(int index) { m_currentIndex = index; }
        virtual int GetCurrentIndex() const override { return m_currentIndex; }
        
        virtual bool SetColumnCount(int columnCount) override;
        virtual int GetColumnCount() const override;

        virtual const IGalleryItemDefinition* GetCurrentItem() const override;
        virtual IGalleryItemDefinition* GetItem(int index) const override;

        virtual std::vector<IGalleryItemDefinition*> GetAllItems() const override;
        virtual bool AddItem(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip, const std::wstring& iconPath, const Any& userData) override;

        virtual IGalleryItemDefinition* GetItem(const std::wstring& id) const override;

        virtual int GetItemIndex(const std::wstring& id) const override;

        virtual bool InsertItem(const std::wstring& beforeId, const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip, const std::wstring& iconPath, const Any& userData) override;

        virtual bool DeleteItem(const std::wstring& id) override;

        virtual bool DeleteAllItems() override;

        virtual IGalleryEvent* GetEvent() const override { return const_cast<IGalleryEvent*>(m_event.get()); }
        virtual ControlDefinitionType GetType() const override { return ControlDefinitionType::Gallery; }

        IControlDefinitionChangeEvent* GetControlDefinitionChangeEvent() const;
    public:
        virtual std::wstring GetId() const override { return m_pControlDefinition->GetId(); }
        virtual std::wstring GetCaption() const override { return m_pControlDefinition->GetCaption(); }
        virtual std::wstring GetTooltip() const override { return m_pControlDefinition->GetTooltip(); }
        virtual bool IsVisible() const override { return true; }
        virtual bool IsEnabled() const override { return true; }
    private:
        OwnerPtr<ControlDefinition> m_pControlDefinition;
        int m_itemWidth;
        int m_itemHeight;
        int m_currentIndex;
        int m_columnCount;
        OwnerPtr<IGalleryEvent> m_event;
        //std::vector<WeakPtr<IGalleryItemDefinition>> m_pGalleryItems;

        // 通知使用该定义的控件的事件
        OwnerPtr<ControlDefinitionChangeEvent> m_pDefinitionChangeEvent;

        std::vector<OwnerPtr<GalleryItemDefinition>> m_pGalleryItems;
    };
}

